//
//  LMLabelSelectView.m
//  LMLabelSelect
//
//  Created by LM on 2022/11/30.
//

#import "LMLabelSelectView.h"
#import "LMLabelSelectCollectionViewCell.h"
#import "UIColor+Theme.h"

#define WS(weakself) __weak typeof (self) weakself = self;

#define KSCREENW [UIScreen mainScreen].bounds.size.width
#define KSCREENH [UIScreen mainScreen].bounds.size.height

@implementation LMLabelSelectView

- (void)awakeFromNib{
    [super awakeFromNib];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self addSubview:self.labelCollectionView];
    }

    return self;
}

- (void)loadCollection{
    [self.labelCollectionView reloadData];
}

- (void)loadDataWithTitles:(NSArray*)titles{
    self.titles = titles;
    self.lastModel = self.titles.firstObject;
    if (self.lastModel.bgColor){
        self.backgroundColor = [UIColor colorWithHexString:self.lastModel.bgColor];
    }
    [self.labelCollectionView reloadData];
    [self.labelCollectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (LMBaseCollectionView*)labelCollectionView{
    WS(weakself);
    if (!_labelCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        _labelCollectionView = [[LMBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, KSCREENW, 45) collectionViewLayout:layout];
        _labelCollectionView.showsHorizontalScrollIndicator = NO;
        _labelCollectionView.backgroundColor = [UIColor clearColor];
        _labelCollectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
        //  注册        
        [_labelCollectionView registerClass:[LMLabelSelectCollectionViewCell class] forCellWithReuseIdentifier:@"LMLabelSelectCollectionViewCell"];

        _labelCollectionView.numberOfItemsBlock = ^NSInteger(UICollectionView *collectionView, NSInteger section) {
            return weakself.titles.count;
        };
        _labelCollectionView.sizeForItemBlock = ^CGSize(UICollectionView *collectionView, UICollectionViewLayout *collectionViewLayout, NSIndexPath *indexPath) {
            LMLabelSelectModel *model = [weakself.titles objectAtIndex:indexPath.item];
            return CGSizeMake(model.width, 45);
        };

        _labelCollectionView.cellForItemsBlock = ^UICollectionViewCell *(UICollectionView *collectionView, NSIndexPath *indexPath) {
            LMLabelSelectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LMLabelSelectCollectionViewCell" forIndexPath:indexPath];
            if (!cell){
                cell = [[LMLabelSelectCollectionViewCell alloc]init];
            }
            LMLabelSelectModel *model = [weakself.titles objectAtIndex:indexPath.item];
            cell.titleLab.text = model.text;

            if (model.isSelected){
                cell.titleLab.textColor = [UIColor colorWithHexString:model.choseTextColor];
                cell.titleLab.font =  [UIFont fontWithName:@"PingFangSC-Semibold" size:model.choseTextFont];
                cell.titleLab.alpha = 1.0;
                cell.iconBgImageView.hidden = NO;
                cell.lineView.hidden = NO;
                cell.lineView.backgroundColor = [UIColor colorWithHexString:model.lineColor];
            }else{
                cell.titleLab.textColor = [UIColor colorWithHexString:model.textColor];
                cell.titleLab.font = [UIFont fontWithName:@"PingFangSC-Regular" size:model.textFont];
                cell.titleLab.alpha = model.textOpacity/100;
                cell.iconBgImageView.hidden = YES;
                cell.lineView.hidden = YES;

            }
            return cell;
        };

        _labelCollectionView.didSelectItemBlock = ^(UICollectionView *collectionView, NSIndexPath *indexPath) {
            [weakself moveScrollViewWithIndex:indexPath.item isDidCell:YES];
        };
        
        _labelCollectionView.scrollViewDidScroll = ^(UIScrollView *scrollView) {

        };
    }
    return _labelCollectionView;
}

// index 下标
// isDidCell yes 按钮点击  no 非按钮点击触发 （如：滚动视图后联动按钮选择）
- (void)moveScrollViewWithIndex:(NSInteger)index isDidCell:(BOOL)isDidCell{
    
    self.lastModel.isSelected = NO;
    LMLabelSelectModel *model = [self.titles objectAtIndex:index];
    model.isSelected = YES;
    [self.labelCollectionView reloadData];
    self.lastModel = model;
    
    LMLabelSelectCollectionViewCell * cell = (LMLabelSelectCollectionViewCell*)[self.labelCollectionView collectionView:self.labelCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];//即为要得到的cell
    // 内容长度
    CGFloat collectionViewContentWidth = self.labelCollectionView.contentSize.width;
    // 当前点击item在collectionView所在位置
    CGFloat collectionX = cell.frame.origin.x; // cell 坐标点
    CGFloat collectionCellWidth = cell.frame.size.width;// cell的长度
    // 判断内容长度是否大于屏幕宽度
    if (collectionViewContentWidth > KSCREENW){
        // 左右中间处理
        if ((collectionX+collectionCellWidth) <= KSCREENW/2){// 左侧临界点
            [self.labelCollectionView setContentOffset:CGPointMake(0, 0) animated:YES];
        }

        if ((collectionX+collectionCellWidth) > KSCREENW/2){ // 右侧临界点
            [self.labelCollectionView setContentOffset:CGPointMake(collectionX+collectionCellWidth/2-KSCREENW/2, 0) animated:YES];
        }
        
        if (collectionViewContentWidth - (collectionX+collectionCellWidth) <= KSCREENW/2){ // 中间段处理
            [self.labelCollectionView setContentOffset:CGPointMake(collectionViewContentWidth-KSCREENW, 0) animated:YES];
        }
    }
    self.scrollBlock(index, isDidCell);
}


@end
